package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.bean.WeatherTypeBean;
import com.qiantu.api.entity.WeatherValueBean;

import java.util.List;

public final class WeatherTypeAdapter extends AppAdapter<WeatherTypeBean> {

    private WeatherTypeBean selected = null;
    private List<WeatherValueBean> selectedValues;
    public WeatherTypeAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView tvType;
        private final TextView checkView;

        private ViewHolder() {
            super(R.layout.item_weather_type);
            tvType = findViewById(R.id.tv_type);
            checkView = findViewById(R.id.check_view);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
//            if(!checkBox.isChecked()){
//                checkBox.setChecked(true);
//            }
//            WeatherTypeBean data = (WeatherTypeBean) view.getTag();
//            selected = data;
//            notifyDataSetChanged();
            super.onClick(view);
        }

        @Override
        public void onBindView(int position) {
            WeatherTypeBean weatherType = getItem(position);
            int nameRes = getResources().getIdentifier(weatherType.getName(), "string", getContext().getPackageName());
            tvType.setText(nameRes);
            int imageRes = AppApplication.getInstance().getResourceId(weatherType.getImage(), "attr");
            tvType.setCompoundDrawablesWithIntrinsicBounds(AppApplication.getInstance().getThemeDrawable(imageRes), null, null, null);
            itemView.setTag(getItem(position));
            if(selected != null && selected.equals(getItem(position))){
                checkView.setSelected(true);
                if(selectedValues != null && selectedValues.size() > 0){
                    StringBuilder stringBuilder = new StringBuilder();
                    for(WeatherValueBean value : selectedValues){
                        for(WeatherValueBean weatherValue : weatherType.getValues()){
                            if(value.getValue() == weatherValue.getValue()){
                                int res = getResources().getIdentifier(weatherValue.getName(), "string", getContext().getPackageName());
                                stringBuilder.append(getString(res)).append("、");
                            }
                        }
                    }
                    if(stringBuilder.length() > 0){
                        checkView.setText(stringBuilder.substring(0, stringBuilder.length() - 1));
                    }
                }
            }else{
                checkView.setSelected(false);
                checkView.setText(null);
            }
        }
    }

    public WeatherTypeBean getSelected() {
        return selected;
    }

    public void checkSelected(WeatherTypeBean selected){
        if(selected != null){
            this.selected = selected;
            notifyDataSetChanged();
        }
    }

    public void checkSelected(int weatherType){
        for(WeatherTypeBean weatherTypeBean : getData()){
            if(weatherType == weatherTypeBean.getType()){
                this.selected = weatherTypeBean;
                notifyDataSetChanged();
                break;
            }
        }
    }

    public void setSelectedValues(List<WeatherValueBean> weatherValues){
        selectedValues = weatherValues;
    }

    public void setSelected(WeatherTypeBean selected){
        if(selected != null){
            this.selected = selected;
        }
    }
}